#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>



#define N 32
#define R 1//注册
#define L 2//登录
#define Q 3//查询
#define H 4//历史记录

//定义通信双方的信息结构体
typedef struct{
    int type;  
    char name[64];
    char data[128];
    }MSG;

int do_register(int sockfd,MSG *msg)
{
    msg->type=R;
    scanf("%s",msg->name);
    getchar();//读取输入缓冲区中的换行符,清空缓冲区
    printf("Input passwd:");
    scanf("%s",msg->data);
    //发送注册信息
    if(send(sockfd,msg,sizeof(MSG),0)<0)
    {
        perror("send\n");
        printf("Register error!\n");
        return -1;
    }
    //接收注册结果
    if(recv(sockfd,msg,sizeof(MSG),0)<0)
    {
        perror("recv\n");
        return -1;
    }
    //ok!注册成功或者注册失败
    printf("%s\n",msg->data);
    return 0;
}

int do_login(int sockfd,MSG *msg)
{
    msg->type=L;
    printf("Input name:");
    scanf("%s",msg->name);
    getchar();//读取输入缓冲区中的换行符,清空缓冲区
    printf("Input passwd:");
    scanf("%s",msg->data);
    //发送登录信息
    if(send(sockfd,msg,sizeof(MSG),0)<0)
    {
        perror("send\n");
        printf("Login error!\n");
        return -1;
    }
    //接收登录结果
    if(recv(sockfd,msg,sizeof(MSG),0)<0)
    {
        perror("recv\n");
        return -1;
    }
    //ok!登录成功或者登录失败
    if (strcmp(msg->data,"OK")==0)
    {
        printf("Login success!\n");
    
        return 1;
    }
    else{
        printf("%s\n",msg->data);
        return 0;
    }
    return 0;


}

int do_query(int sockfd,MSG *msg)


{ 
    msg->type=Q;
    puts(".......................");
    while(1)
    {
        printf("Input word:");
        scanf("%s",msg->data);
        getchar();//读取输入缓冲区中的换行符,清空缓冲区
        if(strncmp(msg->data,"#",1)==0)//输入 # 退出查询
        {
            break;
        }
        //发送查询信息
        if(send(sockfd,msg,sizeof(MSG),0)<0)
        {
            perror("send\n");
            printf("Query error!\n");
            return -1;
        }
        //接收查询结果
        if(recv(sockfd,msg,sizeof(MSG),0)<0)
        {
            perror("recv\n");
            return -1;
        }
        printf("%s\n",msg->data);

    }
}
int do_history(int sockfd,MSG *msg)
{
    msg->type=H;
    //发送查询信息
    if(send(sockfd,msg,sizeof(MSG),0)<0)
    {
        perror("send\n");
        printf("Query error!\n");
        return -1;
    }
    //接收查询结果
    if(recv(sockfd,msg,sizeof(MSG),0)<0)
    {
        perror("recv\n");
        return -1;
    }
    printf("%s\n",msg->data);
}
// ./server 192.168.118.135 10000
int main(int argc,const char *argv[])
{
    struct sockaddr_in serveraddr;
    MSG msg;
    //如果传递参数不为3个，则提示用户输入正确的参数
    if(argc != 3)
    {
        printf("Usage:%s <serverIP> <PORT>\n",argv[0]);
        return -1;
    }

    //创建流式套接字
    int sockfd;//套接字描述符
    if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
    {
        perror("sockfd\n");
        return -1;
    }
    //初始化服务器的IP地址和端口号
    bzero(&serveraddr,sizeof(serveraddr));
    serveraddr.sin_family=AF_INET;
    serveraddr.sin_addr.s_addr=inet_addr(argv[1]);
    serveraddr.sin_port=htons(atoi(argv[2])); 
    //连接服务器
    if(connect(sockfd,(struct sockaddr *)&serveraddr,sizeof(serveraddr))<0)
    {
        perror("connect\n");
        return -1;
    }
    while(1)
    {
        printf("*****************************************\n");
        printf("* 1.register      2.login          3.quit*\n");
        printf("*****************************************\n");
        printf("please choose:");
        int choose;
        scanf("%d",&choose);
        //读取输入缓冲区中的换行符,清空缓冲区
        getchar();
        //while(getchar()!='\n');
        switch(choose)
        {
            case 1:
                do_register(sockfd,&msg);
                break;
            case 2:
                if(do_login(sockfd,&msg)==1)
                {
                    goto next;
                }
                break;
            case 3:
            //关闭套接字
                close(sockfd);
                exit(0);
                break;
            default:
                printf("Input error!\n");
                break;
        }


    }

next:
    while (1)
    {
        printf("*****************************************\n");
        printf("* 1.query         2.history        3.quit*\n");
        printf("*****************************************\n");
        printf("please choose:");
        int choose;
        scanf("%d",&choose);
        //读取输入缓冲区中的换行符,清空缓冲区
        getchar();
        
        switch (choose)
        {
        case 1:
            do_query(sockfd,&msg);
            break;
        case 2:
            do_history(sockfd,&msg);
            break;
        case 3:
            close(sockfd);
            exit(0);
            break;
        default:
            printf("Input error!\n");
            break;
        }
    }
    
    
    return 0;
}
